package com.ruoyi.web.controller.systembasic;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ServletUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mes.systembasic.domain.SmesProduct;
import com.ruoyi.mes.systembasic.service.ISmesProductService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品管理Controller
 *
 * @author simpsense
 * @date 2020-11-26
 */
@RestController
@RequestMapping("/systembasic/product")
public class SmesProductController extends BaseController
{
    @Autowired
    private ISmesProductService smesProductService;

    /**
     * 查询产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(SmesProduct smesProduct)
    {
        startPage();
        List<SmesProduct> list = smesProductService.selectSmesProductList(smesProduct);
        return getDataTable(list);
    }

    /**
     * 导出产品管理列表
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SmesProduct smesProduct)
    {
        List<SmesProduct> list = smesProductService.selectSmesProductList(smesProduct);
        ExcelUtil<SmesProduct> util = new ExcelUtil<SmesProduct>(SmesProduct.class);
        return util.exportExcel(list, "product");
    }

    /**
     * 获取产品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:query')")
    @GetMapping(value = "/{productNo}")
    public AjaxResult getInfo(@PathVariable("productNo") String productNo)
    {
        return AjaxResult.success(smesProductService.selectSmesProductById(productNo));
    }

    /**
     * 新增产品管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:add')")
    @Log(title = "产品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmesProduct smesProduct)
    {
        smesProduct.setCreateBy(SecurityUtils.getUsername());
        return toAjax(smesProductService.insertSmesProduct(smesProduct));
    }

    /**
     * 修改产品管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:edit')")
    @Log(title = "产品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmesProduct smesProduct)
    {
        smesProduct.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(smesProductService.updateSmesProduct(smesProduct));
    }

    /**
     * 删除产品管理
     */
    @PreAuthorize("@ss.hasPermi('systembasic:product:remove')")
    @Log(title = "产品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productNos}")
    public AjaxResult remove(@PathVariable String[] productNos)
    {
        return toAjax(smesProductService.deleteSmesProductByIds(productNos));
    }
}
